@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/apply/withdraw">提现申请</a></li>
                <li class="active">申请列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline" action="" method="get">
                    <div class="form-group">
                        <input type="text" class="form-control" name="name" placeholder="申请人" value="">
                    </div>
                    <div class="form-group">
                        <select class="form-control" name="type">
                            <option value="">申请状态</option>
                            <option value="0">未打款</option>
                            <option value="1">已打款</option>
                            <option value="2">拒绝</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="startTime" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="开始时间" value="">
                    </div>&nbsp;至&nbsp;
                    <div class="form-group">
                        <input type="text" class="form-control" name="endTime" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"  placeholder="结束时间" value="">
                    </div>
                    <button type="submit" class="btn btn-default">筛选</button>
                </form>
            </div>
            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">流水列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>用户名</th>
                                <th>手机号</th>
                                <th>申请时间</th>
                                <th>金额</th>
                                <th>实付金额</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($applies as $apply)
                                <tr>
                                    <td>{{$apply->id}}</td>
                                    <td>{{$apply->getUser($apply->user_id)?$apply->getUser($apply->user_id)->name:''}}</td>
                                    <td>{{$apply->getUser($apply->user_id)?$apply->getUser($apply->user_id)->mobile:''}}</td>
                                    <td>{{$apply->created_at}}</td>
                                    <td>{{$apply->money}}</td>
                                    <td>{{$apply->real_money}}</td>
                                    <td>{{App\Contant\ApplyStatus::trans($apply->state)}}</td>
                                        <td>
                                            <a href="/apply/detail?id={{$apply->id}}" class="btn btn-info btn-sm">预览</a>|
                                            <a href="/apply/agree?id={{$apply->id}}" class="btn btn-primary btn-sm">同意打款</a>|
                                            @if($apply->state == 2)
                                                <button type="button" class="btn btn-success btn-sm">已拒绝</button>
                                            @else
                                                <a class="btn btn-danger apply_{{$apply->id}}" data-toggle="modal" data-whatever="{{$apply->id}}" data-target="#exampleModal" href="#">拒绝</a>
                                            @endif
                                        </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {{$applies->render()}}
        </div>
    </div>
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">申请操作</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" class="applyId" name="apply_id" value=""/>
                        <div class="form-group">
                            <label for="message-text" class="control-label">拒绝理由:</label>
                            <textarea class="form-control" id="reason"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" onclick="refuseReason();" class="btn btn-primary">提交</button>
                </div>
            </div>
        </div>
    </div>
    <script>
        $('#exampleModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            var applyId = button.data('whatever');
            var modal = $(this);
            modal.find('.modal-body input.applyId').val(applyId);
        })
        function refuseReason(){
            var reason = $("#reason").val();
            var _token = '{{csrf_token()}}';
            var applyId = $("input[name='apply_id']").val();
            var data = {reason:reason,_token:_token,id:applyId};
            $.ajax({
                url:'/apply/deny',
                data:data,
                type:'POST',
                dataType:'json',
                success:function(res){
                    if(res.code == 0){
                        swal('success','操作成功');
                        $('#exampleModal').modal('hide');
                        var className = ".apply_"+applyId;
                        var parentNode = $(className).parent();
                        var stateNode = $(parentNode).prev();
                        $(className).remove();
                        $(parentNode).append('<button type="button" class="btn btn-success btn-sm">已拒绝</button>');
                        $(stateNode).text('拒绝');
                    }else{
                        swal('error',res.message);
                        $('#exampleModal').modal('hide');
                    }

                }
            });
        }
    </script>
@endsection